package com.fpt.edu.jornee.account;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.fpt.edu.jornee.R;
import com.fpt.edu.jornee.utils.JSONParser;

public class RegisterFragment extends Fragment {

	Button registerBtn;
	EditText userName;
	EditText password;
	EditText email;

	JSONParser parser;

	public RegisterFragment() {
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_register_layout,
				container, false);

		parser = new JSONParser();
		userName = (EditText) rootView.findViewById(R.id.reg_fullname);
		password = (EditText) rootView.findViewById(R.id.reg_password);
		email = (EditText) rootView.findViewById(R.id.reg_email);

		registerBtn = (Button) rootView.findViewById(R.id.btnRegister);
		registerBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LoadResultRegister loadResultRegister = new LoadResultRegister();
				loadResultRegister.execute();
			}
		});
		setHasOptionsMenu(true);
		return rootView;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.outside, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	public JSONObject register() {

		JSONObject jsonObject = null;

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);

		nameValuePairs.add(new BasicNameValuePair("id", userName.getText()
				.toString()));
		nameValuePairs.add(new BasicNameValuePair("pass", password.getText()
				.toString()));
		nameValuePairs.add(new BasicNameValuePair("email", email.getText()
				.toString()));

		jsonObject = parser.makeHttpRequest(
				"http://192.168.11.21:3000/register", "POST", nameValuePairs);

		return jsonObject;
	}

	private class LoadResultRegister extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... place) {

			JSONObject jsonObject = null;
			String result = null;
			try {
				jsonObject = register();
				result = jsonObject.getString("status");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;

		}

		@Override
		protected void onPostExecute(String result) {

			System.out.println(result + "1111111111111");

		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}

}
